package net.tngou.crawler.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.collections.map.HashedMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.ArrangeData;
import net.tngou.c4j.ml.Forecast;
import net.tngou.crawler.dao.DataDao;
import net.tngou.crawler.enums.StateEnum;
import net.tngou.crawler.jdbc.OrderType;
import net.tngou.crawler.jdbc.PageUtil;
import net.tngou.crawler.pojo.Data;
import net.tngou.crawler.pojo.Ml;
import net.tngou.crawler.pojo.POJO;
import net.tngou.crawler.pojo.Urldata;
import net.tngou.crawler.pojo.Web;
import net.tngou.crawler.pojo.Weburl;
import net.tngou.crawler.util.HttpConfig;
import net.tngou.crawler.util.HttpUtil;
import net.tngou.crawler.util.StringUtil;

public class DataService extends BaseService {

	public PageUtil	 getPage(int page,int size,int stete) {
		DataDao dao = new DataDao();
		return new PageUtil(dao.list(page, size,stete), page, size, dao.total(stete));
	}
	
	public StateEnum upload(Urldata urldata) {
		Data data = new Data().get("urldata="+urldata.getId());
		if(data==null)return StateEnum.等待上传;
		return	upload(data);
	}
	
	public StateEnum upload(Data data) {
		Urldata bean = new Urldata().get(data.getUrldata());
		if(bean==null||bean.getState()!=StateEnum.等待上传.ordinal())return StateEnum.等待上传;
		List<Data> list = new ArrayList<>();
		if(StringUtil.isNotEmpty(data.getEqual()))
		{
			List<Data> ls = (List<Data>) getList("equal='"+data.getEqual()+"'", "seq", OrderType.ASC, Data.class);
			int total = new DataDao().totalstate(data.getEqual(), StateEnum.等待上传.ordinal());
			if(ls.size()!=total)return StateEnum.等待上传;	
			list.addAll(ls);
			
		}else
		{
			list.add(data);
		}	
		list.forEach(e->{
			bean.update(new HashMap<String,Object>() {{put("state", StateEnum.上传中.ordinal());}}, e.getUrldata());
		});
		JSONObject json=getData(data);
		if(json==null)
		{
			list.forEach(e->{
				bean.update(new HashMap<String,Object>() {{put("state", StateEnum.上传失败.ordinal());}}, e.getUrldata());
			});
			return StateEnum.上传失败;	
		}
		StateEnum state=updata(json);
		list.forEach(e->{
			bean.update(new HashMap<String,Object>() {{put("state", state.ordinal());}}, e.getUrldata());
		});	
		return state;
		
	}
	
	/**
	 * 取得上传数据
	 * @param data
	 * @return
	 */
	public JSONObject getData(Data data) {
		Urldata urldata = new Urldata().get(data.getUrldata());
		if(urldata==null||urldata.getState()!=StateEnum.上传中.ordinal()) return null;
		Weburl weburl = new Weburl().get(urldata.getWeburl());
		if(weburl==null)return null;
		Web web = new Web().get(weburl.getWeb());
		if(web==null)return null;
		
		JSONObject json = new JSONObject();
		json.put("web", web);
		json.put("weburl", weburl);
		
		if(StringUtil.isNotEmpty(data.getEqual()))
		{
	
			@SuppressWarnings("unchecked")
			List<Data> list = (List<Data>) getList("equal='"+data.getEqual()+"'", "seq", OrderType.ASC, Data.class);
//			int total = new DataDao().totalstate(data.getEqual(), StateEnum.上传中.ordinal());
//			if(list.size()!=total)return null;			
			JSONObject jsondata = ArrangeData.getJSON(list);
			json.put("data", jsondata);
			
		}else
		{
			String value = data.getValue();
			JSONObject jsondata =(JSONObject) JSON.parse(value);
			json.put("data", jsondata);
			
		}
		return json;
		
	}
	
	
	public void checkData(Data data) {
		Urldata urldata = new Urldata().get(data.getUrldata());
		if(urldata==null||urldata.getState()!=StateEnum.爬取完成.ordinal()) return;
		Weburl weburl = new Weburl().get(urldata.getWeburl());
		if(weburl==null)return ;
		JSONObject json =(JSONObject) JSON.parse(data.getValue());
		
		
	}
	
	
	private StateEnum updata(JSONObject reqBody) {
		HttpConfig config = HttpConfig.getInstance();
		JSONObject json = HttpUtil.post(config.getUploadurl(), reqBody, 12*1000);
		if(json!=null&&json.getBoolean("status"))
		{		
			return StateEnum.valueOf(json.getString("msg"));
		}
		return StateEnum.上传失败;
		
	}
	
	
	
}
